package com.webchat.connect.websocket.config;

import com.webchat.connect.websocket.handler.ChatWebSocketEndPointServletHandler;
import com.webchat.connect.websocket.handler.GroupVideoWebSocketEndPointServletHandler;
import com.webchat.connect.websocket.handler.P2PVideoWebSocketEndPointServletHandler;
import com.webchat.connect.websocket.interceptor.ChatWebSocketEndPointServletInterceptor;
import com.webchat.connect.websocket.interceptor.GroupVideoWebSocketEndPointServletInterceptor;
import com.webchat.connect.websocket.interceptor.VideoWebSocketEndPointServletInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConnectServletConfig implements WebSocketConfigurer {

    private static final String CHAT_WEBSOCKET_PATH = "/connect-service/ws/chat/{bizCode}/{userId}";

    private static final String P2P_VIDEO_WEBSOCKET_PATH = "/connect-service/ws/p2p/video/{bizCode}/{userId}";

    private static final String GROUP_VIDEO_WEBSOCKET_PATH = "/connect-service/ws/group/video/{bizCode}/{groupId}/{userId}";

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        /**
         * 注册对话场景WS端点服务
         */
        registry.addHandler(chatWebSocketEndPointServletHandler(), CHAT_WEBSOCKET_PATH)
                .setAllowedOrigins("*")
                .addInterceptors(new ChatWebSocketEndPointServletInterceptor());
        /**
         * 注册点对点音视频通话ws信令服务
         */
        registry.addHandler(p2PVideoWebSocketEndPointServletHandler(), P2P_VIDEO_WEBSOCKET_PATH)
                .setAllowedOrigins("*")
                .addInterceptors(new VideoWebSocketEndPointServletInterceptor());
        /**
         * 注册群聊多人音视频通话ws信令服务
         */
        registry.addHandler(groupVideoWebSocketEndPointServletHandler(), GROUP_VIDEO_WEBSOCKET_PATH)
                .setAllowedOrigins("*")
                .addInterceptors(new GroupVideoWebSocketEndPointServletInterceptor());
    }

    @Bean
    public ChatWebSocketEndPointServletHandler chatWebSocketEndPointServletHandler() {
        return new ChatWebSocketEndPointServletHandler();
    }

    @Bean
    public P2PVideoWebSocketEndPointServletHandler p2PVideoWebSocketEndPointServletHandler() {
        return new P2PVideoWebSocketEndPointServletHandler();
    }

    @Bean
    public GroupVideoWebSocketEndPointServletHandler groupVideoWebSocketEndPointServletHandler() {
        return new GroupVideoWebSocketEndPointServletHandler();
    }
}